{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Intro to Vectors with PyTorch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import sys\n",
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "first element = 0.11 third_element = 0.98\n",
      "last_element = 0.63 second_last_element = 0.87\n",
      "second_to_fifth_elements: tensor([0.0100, 0.9800, 0.1200], dtype=torch.float64)\n",
      "first_to_third_elements: tensor([0.1100, 0.0100], dtype=torch.float64)  Last two elements: tensor([0.8700, 0.6300], dtype=torch.float64)\n",
      "Number of elements in v: 15\n",
      "tensor([0.1100, 0.0100, 0.9800, 0.1200, 0.9800, 0.8500, 0.0300, 0.5500, 0.4900,\n",
      "        0.9900, 0.0200, 0.3100, 0.5500, 0.8700, 0.6300], dtype=torch.float64)\n",
      "tensor([0.1100, 0.0100, 0.9800, 0.1200, 0.9800, 0.8500, 0.0300, 0.5500, 0.4900,\n",
      "        0.9900, 0.0200, 0.3100, 0.5500, 0.8700, 0.6300], dtype=torch.float64)\n",
      "tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
      "       dtype=torch.float64)\n"
     ]
    }
   ],
   "source": [
    "# A torch tensor represents a multi-dimensional array (element type\n",
    "# will be float by default, here we are forcing float64, aka double).\n",
    "# A vector is a special case (1D array). It can be initialized\n",
    "# directly by specifying the values.\n",
    "v = torch.tensor([0.11, 0.01, 0.98, 0.12, 0.98, 0.85, 0.03,\n",
    "                  0.55, 0.49, 0.99, 0.02, 0.31, 0.55, 0.87,\n",
    "                  0.63], dtype=torch.float64)\n",
    "\n",
    "\n",
    "# Individual elements of the vector can be accessed through\n",
    "# the square bracket operator with indices of the desired\n",
    "# element  within thosesquare brackets.\n",
    "# Index starts from zero, not one.\n",
    "first_element = v[0]\n",
    "third_element = v[2]\n",
    "print(\"first element = {} third_element = {}\".\\\n",
    "      format(first_element, third_element))\n",
    "\n",
    "\n",
    "# Negative indices are to be counted from the end of array.\n",
    "# E.g., -1 refers to the last array element.\n",
    "#       -2 refers to the second from last element.\n",
    "last_element = v[-1]\n",
    "second_last_element = v[-2]\n",
    "print(\"last_element = {} second_last_element = {}\".\\\n",
    "      format(last_element, second_last_element))\n",
    "\n",
    "# A range of elements can be sliced off a vector using the\n",
    "# colon operator\n",
    "second_to_fifth_elements = v[1:4]\n",
    "print(\"second_to_fifth_elements: {}\".format(second_to_fifth_elements))\n",
    "\n",
    "# If nothing is specified on the left of the arrow, it implies beginning\n",
    "# of array.\n",
    "# If nothing is specified after the  colon it implies end of array.\n",
    "first_to_third_elements = v[:2]\n",
    "last_two_elements = v[-2:]\n",
    "print(\"first_to_third_elements: {}  Last two elements: {}\".\\\n",
    "      format(first_to_third_elements, last_two_elements))\n",
    "\n",
    "# Size of the vector\n",
    "num_elements_in_v = len(v)\n",
    "print(\"Number of elements in v: {}\".format(num_elements_in_v))\n",
    "\n",
    "# A torch tensor can also be initialized from a numpy array.\n",
    "u = np.array([0.11, 0.01, 0.98, 0.12, 0.98, 0.85, 0.03,\n",
    "              0.55, 0.49, 0.99, 0.02, 0.31, 0.55, 0.87,\n",
    "              0.63])\n",
    "u = torch.from_numpy(u)\n",
    "\n",
    "# One can add subtract tensors. Here we do diff = v1 - u.\n",
    "# Since v1 and u contain the same values, their difference\n",
    "# is all zeros.\n",
    "diff = v.sub(u)\n",
    "print(u)\n",
    "print(v)\n",
    "print(diff)\n",
    "\n",
    "# torch tensors can be converted to numpy arrays too.\n",
    "u1 = u.numpy()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
